{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "078aea82",
   "metadata": {},
   "source": [
    "### week02\n",
    "#### 记录人：zyp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24922664",
   "metadata": {},
   "source": [
    "# 上周回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b244914e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278053,78dcf17f-d99a-4375-a7cd-dd0fea6d1871',\n",
       " 'time_used': 578,\n",
       " 'faces': [{'face_token': '29d173f04ab91c854d0f204fce4e747f',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 输入我们需要的API网站注册的API_key\n",
    "API_Key = \"xieV308Gc4URYyD4s9t1vFbmoQRQElJ5\"\n",
    "API_secret = \"v-J8Z45IUMmnECIeflzsMRSm2orCGVS0\"\n",
    "\n",
    "# 3. 准备 API接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7\"}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "882c071d",
   "metadata": {},
   "source": [
    "# 实践练习2：多张人脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "75579c31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d475d06a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'xieV308Gc4URYyD4s9t1vFbmoQRQElJ5'\n",
    "API_sercret = 'v-J8Z45IUMmnECIeflzsMRSm2orCGVS0'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.dzsc.com%2Fdata%2Fuploadfile%2F20119210321247.jpg&refer=http%3A%2F%2Fwww.dzsc.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649905380&t=032add4c4b02facb19da445559763b1b'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a0918ced",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec1415fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278224,983f4634-f2d4-4fc4-9458-24c441f37235',\n",
       " 'time_used': 309,\n",
       " 'faces': [{'face_token': '51da90958de34e88559f2964de88c106',\n",
       "   'face_rectangle': {'top': 234, 'left': 5, 'width': 119, 'height': 119}},\n",
       "  {'face_token': 'd38345ef917818487e1ef4dc27aeb167',\n",
       "   'face_rectangle': {'top': 239, 'left': 491, 'width': 111, 'height': 111}},\n",
       "  {'face_token': '89227a292928d1318de2fb8ffa92ad9b',\n",
       "   'face_rectangle': {'top': 231, 'left': 253, 'width': 110, 'height': 110}},\n",
       "  {'face_token': 'e3772474c2eb43e4b95905d51b31d620',\n",
       "   'face_rectangle': {'top': 241, 'left': 373, 'width': 109, 'height': 109}},\n",
       "  {'face_token': 'edb761d5ab26bf76d13c4b13e0fef9e7',\n",
       "   'face_rectangle': {'top': 235, 'left': 130, 'width': 109, 'height': 109}},\n",
       "  {'face_token': '72c16c5933d869cfb3ae9ea1084e787b',\n",
       "   'face_rectangle': {'top': 53, 'left': 255, 'width': 107, 'height': 107}},\n",
       "  {'face_token': 'c87702b6e9278031e4551eee2086c952',\n",
       "   'face_rectangle': {'top': 54, 'left': 378, 'width': 107, 'height': 107}},\n",
       "  {'face_token': 'c33070fdd6d5434e5210d53681684b16',\n",
       "   'face_rectangle': {'top': 55, 'left': 133, 'width': 105, 'height': 105}},\n",
       "  {'face_token': '8a259fac8957ec9eab6b8e722ae9acae',\n",
       "   'face_rectangle': {'top': 54, 'left': 499, 'width': 105, 'height': 105}},\n",
       "  {'face_token': '27bcfca6afc85584f18112d770d16ce7',\n",
       "   'face_rectangle': {'top': 60, 'left': 21, 'width': 99, 'height': 99}}],\n",
       " 'image_id': '2RzNr0mI98SpgUwQMrZemw==',\n",
       " 'face_num': 10}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f6479c4",
   "metadata": {},
   "source": [
    "# 人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "faa9400e",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "00d36233",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8df30d4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'51da90958de34e88559f2964de88c106'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "58eff382",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'xieV308Gc4URYyD4s9t1vFbmoQRQElJ5'\n",
    "API_sercret = 'v-J8Z45IUMmnECIeflzsMRSm2orCGVS0'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'b899e2401d86532e9e78b980d525c578',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6338cc65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "97a0ab18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 73,\n",
       " 'error_message': 'INVALID_FACE_TOKEN: b899e2401d86532e9e78b980d525c578',\n",
       " 'request_id': '1648278236,b0c187f5-01aa-4c39-b0bf-8991c5acee51'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cd412ee8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "804b60a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>b899e2401d86532e9e78b980d525c578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>9.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>1.361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>81.032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>6.503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>59.951</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>54.586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.237</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>51.93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>84.751</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>42.108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>0.661</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>15.845812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>1.813057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>5.621018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>15.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>15.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>15.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>96.545</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>1.627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>1.242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.453</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>90.939</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>1.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>6.793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>37.642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>0.142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>0.448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          b899e2401d86532e9e78b980d525c578\n",
       "attributes.emotion.sadness                                                     0.652\n",
       "attributes.emotion.neutral                                                      9.76\n",
       "attributes.emotion.disgust                                                     1.361\n",
       "attributes.emotion.anger                                                       0.537\n",
       "attributes.emotion.surprise                                                   81.032\n",
       "attributes.emotion.fear                                                        0.155\n",
       "attributes.emotion.happiness                                                   6.503\n",
       "attributes.beauty.female_score                                                59.951\n",
       "attributes.beauty.male_score                                                  54.586\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              38\n",
       "attributes.mouthstatus.close                                                  99.762\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                      0.0\n",
       "attributes.mouthstatus.other_occlusion                                         0.237\n",
       "attributes.glass.value                                                        Normal\n",
       "attributes.skinstatus.dark_circle                                              51.93\n",
       "attributes.skinstatus.stain                                                   84.751\n",
       "attributes.skinstatus.acne                                                    42.108\n",
       "attributes.skinstatus.health                                                   0.661\n",
       "attributes.headpose.yaw_angle                                              15.845812\n",
       "attributes.headpose.pitch_angle                                             1.813057\n",
       "attributes.headpose.roll_angle                                              5.621018\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                15.062\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                              15.062\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                            15.062\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                           0.3\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                            96.545\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             0.016\n",
       "attributes.eyestatus.left_eye_status.occlusion                                 1.627\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             1.242\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.118\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                              0.453\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                            90.939\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             0.004\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.957\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             1.031\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.277\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                             6.793\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  37.642\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.504\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.989\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.034\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             0.142\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.425\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.641\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.614\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                             0.448\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             -0.65\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.086\n",
       "face_rectangle.width                                                             119\n",
       "face_rectangle.top                                                               234\n",
       "face_rectangle.left                                                                5\n",
       "face_rectangle.height                                                            119"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43019fa4",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "793cdce3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278248,75ca2aaa-3618-43d0-b68c-14e9bd679114',\n",
       " 'time_used': 721,\n",
       " 'faces': [{'face_token': '457da435e3d27117b5e3aa9f1968ea47',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# image1\n",
    "# 1. 导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 输入我们需要的API网站注册的API_key\n",
    "API_Key = \"xieV308Gc4URYyD4s9t1vFbmoQRQElJ5\"\n",
    "API_secret = \"v-J8Z45IUMmnECIeflzsMRSm2orCGVS0\"\n",
    "\n",
    "# 3. 准备 API接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7\"}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4034a364",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648278250,a306f34e-7b9c-4cbb-8b8c-d90523344c3b',\n",
       " 'time_used': 143,\n",
       " 'faces': [{'face_token': 'a20273161b9b810c2698a614639cd06a',\n",
       "   'face_rectangle': {'top': 262, 'left': 299, 'width': 338, 'height': 338}}],\n",
       " 'image_id': 'IzlLTp6TGVpTdTq0pBGBiw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# image2\n",
    "# 1. 导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 输入我们需要的API网站注册的API_key\n",
    "API_Key = \"xieV308Gc4URYyD4s9t1vFbmoQRQElJ5\"\n",
    "API_secret = \"v-J8Z45IUMmnECIeflzsMRSm2orCGVS0\"\n",
    "\n",
    "# 3. 准备 API接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 调用方法 POST\n",
    "\n",
    "# 5. 准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13929048003%2F1000.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649906645&t=473e9eed056ca57ebeed06cfbdebe645\"}\n",
    "\n",
    "# 6. 发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "34670b8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'xieV308Gc4URYyD4s9t1vFbmoQRQElJ5'\n",
    "API_sercret = 'v-J8Z45IUMmnECIeflzsMRSm2orCGVS0'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':'8db51605cbc1059f7ab93a3ba2c16f66',\n",
    "    'face_token1':'ac18be4950f23b0f537d40c2e1512569'}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "4458a227",
   "metadata": {},
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "95d6cb1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [400]>>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
